﻿using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using OaEntity.Models;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace PermissionAuthenticationDemo.Utility.Service.Impl
{
    public class JWTServiceImpl : IJWTService
    {
        private JWTTokenOptions _JWTTokenOptions;

        private readonly OaContext oaContext;

        public JWTServiceImpl(IOptions<JWTTokenOptions> options, OaContext oaContext)
        {
            _JWTTokenOptions = options.Value;
            this.oaContext = oaContext;
        }

        public string GetToken(string name)
        {
            var claims = new[]
            {
                
                new Claim("id", "11"),
                #region 为授权添加
                new Claim(ClaimTypes.Name, "Name"),
                new Claim("Name", name),
                new Claim("EMail", "2043708045@qq.com"),
                new Claim(ClaimTypes.Email, "xuyun@zhaoxiedu.net"),
                new Claim("Account", "Administrator"),
                new Claim("Age", "22"),
                new Claim("Mobile", "15361713545"),
                new Claim(ClaimTypes.Role, "Admin"),
                new Claim("Role", "User"),
                new Claim("Sex", "1"),
                #endregion
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_JWTTokenOptions.SecurityKey)); //配置文件中的SecurityKey，生成key
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); //加密key

            var nowTime = DateTime.Now;
            var expiration = nowTime.AddHours(1); //设置Token过期时间

            var token = new JwtSecurityToken(
                issuer: _JWTTokenOptions.Issuer, //配置文件中的Issuer
                audience: _JWTTokenOptions.Audience, //配置文件中的Audience
                claims: claims, //用户信息
                expires: expiration, //过期时间
                notBefore: nowTime, //生效时间，立即生效      DateTime.Now.AddMilliseconds(30), //30s后生效
                signingCredentials: creds //加密的key
            );
            string returnToken = new JwtSecurityTokenHandler().WriteToken(token);   //使用JwtSecurityTokenHandler方法来创建token
            return returnToken;
        }

        public string GetToken(int userID)
        {
            var user = oaContext.Tcausers.Where(x => x.UserId == userID).FirstOrDefault();
            if(user != null)
            {
                int id = user.UserId;
                string name = user.UserName;
                string eMail = user.Email;
                string account = user.UserName;
                string mobile = user.Phone;
                int status = (int)user.Status;

                var claims = new[]
                {
                    new Claim("id", userID.ToString()),
                    #region 为授权添加
                    new Claim(ClaimTypes.Sid, id.ToString()),
                    new Claim(ClaimTypes.Name, name),
                    new Claim("Name", name),
                    new Claim("EMail", eMail),
                    new Claim(ClaimTypes.Email, "xuyun@zhaoxiedu.net"),
                    new Claim("Account", account),
                    new Claim("Mobile", mobile),
                    new Claim("Satus", status.ToString()),
                    new Claim("Age", "22"),
                    new Claim(ClaimTypes.Role, "Admin"),
                    new Claim("Role", "User"),
                    new Claim("Sex", "1"),
                    #endregion
                };

                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_JWTTokenOptions.SecurityKey)); //配置文件中的SecurityKey，生成key
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); //加密key

                var nowTime = DateTime.Now;
                var expiration = nowTime.AddHours(1); //设置Token过期时间

                var token = new JwtSecurityToken(
                    issuer: _JWTTokenOptions.Issuer, //配置文件中的Issuer
                    audience: _JWTTokenOptions.Audience, //配置文件中的Audience
                    claims: claims, //用户信息
                    expires: expiration, //过期时间
                    notBefore: nowTime, //生效时间，立即生效      DateTime.Now.AddMilliseconds(30), //30s后生效
                    signingCredentials: creds //加密的key
                );
                string returnToken = new JwtSecurityTokenHandler().WriteToken(token);   //使用JwtSecurityTokenHandler方法来创建token
                
                return returnToken;
            }
            throw new NotImplementedException();
        }
    }
}
